package com.zretc.controller;

import com.zretc.entity.Admin;
import com.zretc.service.AdminLoginService;
import com.zretc.util.CodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminLoginController {
    @Autowired
    private AdminLoginService adminLoginService;
    @RequestMapping("/adminLogin")
    @ResponseBody
    public Map<String,String> adminLogin(String adminId,
                                         String password,
                                         String verify,
                                         HttpServletRequest request){

        Map<String,String> adminMap = new HashMap<>();
        //获取session中的值
        HttpSession session = request.getSession();
        String code = (String) session.getAttribute("code");
        //判断用户传入的验证码是否正确
        if (verify.equals(code)){
            adminMap.put("code","yes");
            Admin admin = adminLoginService.getAdminByIdAndPassword(adminId,password);
            if (admin==null){
                //如果admin为空，说明用户名或者密码错误，没有查到该对象
                adminMap.put("adminObject","no");
                return adminMap;
            }else{
                session.setAttribute("adminId",admin.getAdminId());
                adminMap.put("adminObject","yes");
                //判断该管理员是超级管理员还是普通管理员
                //1.获取该对象的类型
                int adminType= admin.getAdminType();
                if (adminType==1){
                    //如果等于1，说明是超级管理员
                    adminMap.put("adminType","superAdmin");
                }else{
                    //负责0是普通管理员
                    adminMap.put("adminType","commonAdmin");
                }
            }
        }else{
            adminMap.put("code","no");
        }
        return adminMap;
    }

    @RequestMapping("/outLogin")
    public String outLogin(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("adminId");
        return "redirect:../back/login.jsp";
    }

    @RequestMapping("/getVerifyCode")
    public void getVerifyCode(HttpServletRequest request, HttpServletResponse response){
        // 调用工具类生成的验证码和验证码图片
        Map<String, Object> codeMap = CodeUtil.generateCodeAndPic();
        // 将四位数字的验证码保存到Session中。
        HttpSession session = request.getSession();
        session.setAttribute("code", codeMap.get("code").toString());

        // 禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", -1);

        response.setContentType("image/jpeg");

        // 将图像输出到Servlet输出流中。
        ServletOutputStream sos;
        try {
            sos = response.getOutputStream();
            ImageIO.write((RenderedImage) codeMap.get("codePic"), "jpeg", sos);
            sos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
